package com.itlin.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itlin.bean.Dish;
import com.itlin.bean.Employee;
import com.itlin.bean.PageParam;
import com.itlin.common.R;
import com.itlin.dto.DishDto;
import com.itlin.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 所谓伊人
 * @date 2022/3/25 - 15:07
 */
@Slf4j
@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public R addDish(@RequestBody DishDto dishDto) {
        int row = dishService.add(dishDto);

        if (row > 0) {

            //清理某个分类下面的菜品缓存数据
            String key = "dish_" + dishDto.getCategoryId() + "_1";
            redisTemplate.delete(key);

            return R.success("添加菜品成功");
        }

        return R.error("添加菜品失败");
    }

    @GetMapping("/list")
    public R<List<DishDto>> findByCategoryId(long categoryId, Integer status) {

        //动态构造key
        String key = "dish_" + categoryId + "_" + status;//dish_1397844391040167938_1

        //先从redis中获取缓存数据
        String data = (String) redisTemplate.opsForValue().get(key);
        if (data != null) {
            //如果存在，直接返回，无需查询数据库
            return R.success(JSON.parseObject(data, List.class));
        }

        List<DishDto> dishList = dishService.findByCategoryId(categoryId, status);


        //如果不存在，需要查询数据库，将查询到的菜品数据缓存到Redis
        redisTemplate.opsForValue().set(key, JSON.toJSONString(dishList), 60, TimeUnit.MINUTES);

        return R.success(dishList);
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto) {

        int row = dishService.update(dishDto);

        if (row > 0) {

            //清理所有菜品的缓存数据
            Set keys = redisTemplate.keys("dish_*"); //获取所有以dish_xxx开头的key
            redisTemplate.delete(keys); //删除这些key

            return R.success("更新菜品成功");
        }

        return R.error("更新菜品失败");
    }

    @GetMapping("/page")
    public R<IPage<DishDto>> findPage(PageParam pageParam) {

        IPage<DishDto> page = dishService.findPage(pageParam);


        return R.success(page);
    }

    @PostMapping("/status/{status}")
    public R updataStatus(Dish dish, @RequestParam List<Long> ids) {
        int i = dishService.updateStatus(dish, ids);
        if (i == ids.size()) {
            return R.success("批量设置成功");
        }
        return R.error("批量设置失败");
    }

    @DeleteMapping
    public R deleteById(@RequestParam List<Long> ids) {
        int i = dishService.deleteById(ids);
        if (i > 0 && i != 999999999) {
            return R.success("批量删除成功");
        } else if (i == 999999999) {
            return R.error("选中了起售菜品哟");
        } else {
            return R.error("删除失败");
        }
    }
}
